/**
 * Created by 13296 on 2018/7/4.
 */
//当前用户信息
// let current_user ={
//     "user_id":0,
//     "image_url":"https://bpic.588ku.com/back_pic/05/53/30/405af9ab04baed1.jpg!/fw/320/quality/90/unsharp/true/compress/true",
//     "account":0};
// let ready=function (){
//     var storage = window.sessionStorage;
//     try {
//         let user =JSON.parse(storage["current_user"]);
//         if(user != null && user != undefined){
//             current_user = user;
//         }
//         console.log(current_user);
//     }catch(err) {
//         console.log(current_user);
//     }
// };
// ready();

/*插入评论*/
let insertComment=function (comment,method) {
    let s ='<div class="media">'+
        '   <h5>'+comment.account+'</h5>'+
        '   <div class="media-left">'+
        '       <a href="#" style="background:url('+comment.image_url+') no-repeat center;background-size:65px 65px;"> </a>'+
        '   </div>'+
        '   <div class="media-body">'+
        '       <p>'+comment.context+'</p>'+
        '       <span>评论时间 : <a href="">'+comment.comment_date+'</a></span>'+
        '   </div>'+
        '</div>';
    if(method == "append"){
        $(".media-grids").append(s);
    }else {
        $(".media-grids").prepend(s);
        let count = parseInt($("#total_comments").text().split("(")[1].split(')')[0]);//评论总数
        console.log("comments:"+(count+1));
        $("#total_comments").text('All Comments ('+(count+1)+')');//评论总数
    }
};

/*举报模态框*/
let abuseModelView=function () {
    let model = '<div class="mod_pop_action_section" id="myModel" style="position: absolute;display: none;">'+
                '<div class="mod_more_pop">'+
                '   <span></span>'+
                '   <div class="more_pop_info" style="background-color:black;">'+
                '       <div class="pop_option_title">举报视频</div>'+
                '       <div class="pop_option">'+
                '          <input type="radio" name="optionsRadios" id="optionsRadios1" value="1"> 广告'+
                '       </div>'+
                '       <div class="pop_option">'+
                '           <input type="radio" name="optionsRadios" id="optionsRadios2" value="2"> 时政反动'+
                '       </div>'+
                '       <div class="pop_option">'+
                '           <input type="radio" name="optionsRadios" id="optionsRadios3" value="3"> 色情低俗'+
                '       </div>'+
                '       <div class="pop_option">'+
                '           <input type="radio" name="optionsRadios" id="optionsRadios4" value="4"> 暴力血腥'+
                '       </div>'+
                '       <div class="pop_option">'+
                '           <input type="radio" name="optionsRadios" id="optionsRadios5" value="5"> 违法犯罪'+
                '       </div>'+
                '       <div class="pop_option">'+
                '           <input type="radio" name="optionsRadios" id="optionsRadios6" value="6"> 标题夸张'+
                '       </div>'+
                '       <div class="pop_option">'+
                '           <input type="radio" name="optionsRadios" id="optionsRadios7" value="7"> 文不对题'+
                '       </div>'+
                '       <div class="pop_option">'+
                '           <input type="radio" name="optionsRadios" id="optionsRadios8" value="8"> 欺诈恶意营销'+
                '       </div>'+
                '       <div class="pop_option">'+
                '           <input type="radio" name="optionsRadios" id="optionsRadios9" value="9"> 其他'+
                '       </div>'+
                '       <a href="javascript:;" class="pop_btn_em disabled" id="abuse_submit">提交</a>'+
                '   </div>'+
                // '   <div class="more_pop_result" style="display: none;"></div>'+
                '</div>'+
                '</div>';
    $("#abuse_action").append(model);

    //举报模态框
    let clickCount = 0;
    $("#add_abuse").click(function () {
        // console.log("弹出模态框");
        clickCount = (clickCount+1)%2;
        if(clickCount%2==1){
            $("#myModel").css("display","block");
        }else{
            $("#myModel").css("display","none");
        }
    });
};

/*处理热门上升视频*/
let up_next=function () {
    // console.log("最新上升");
    let url = "/video/up_next";
    let method = "GET"
    let data = null;
    ajax_base(data, url, method, "Json").then(function (data) {
            if (data.success) {
                // console.log(data);
                let videos = data.obj;
                // console.log(videos);
                var s = '';
                for(var i = 0; i < videos.length; i++) {
                    s = '<div class="single-right-grids">'+
                        '   <div class="col-md-4 single-right-grid-left">'+
                        '       <a href="single?video_id='+videos[i].video_id+'"><img src="'+videos[i].image_url+'" alt="" /></a>'+
                        '   </div>'+
                        '   <div class="col-md-8 single-right-grid-right">'+
                        '       <a href="single?video_id='+videos[i].video_id+'" class="title">'+videos[i].title+'</a>'+
                        '       <p class="author"><a href="#" class="author">'+videos[i].account+'</a></p>'+
                        '   <p class="views">'+videos[i].views+' views</p>'+
                        '   </div>'+
                        '   <div class="clearfix"> </div>'+
                        '</div>';
                    $(".single-grid-right").append(s);
                }
            }
        }
    );
};
up_next();

/*处理请求*/
let dealGetAndPost=function () {
    //添加评论
    $("#sendComment").click(function () {
        // console.log($("#video_id").val()+' '+$("#user_id").val()+' '+$("#context").val());
        if(current_user.user_id != 0){
            let data =  {
                "video_id":$("#video_id").val(),
                "user_id":$("#user_id").val(),
                "context":$("#context").val(),};
            let method = "POST";
            let url = "/comment/add_comment";

            console.log(data);
            ajax_base(data, url, method, "Json").then(function (data) {
                    if (data.success) {
                        // console.log(data);
                        insertComment(data.obj);
                        $("#context").val("");
                        alert("评论成功！");
                    }
                }
            );
        }else{
            alert("登录后才可以评论!");
        }

    });

    //点击关注/取消关注
    $("#follow").click(function () {
        console.log("点击关注");
        if(current_user.user_id != 0){
            let follow = $("#follow").text();
            let is_follow = 1;
            if(follow == "关注"){
                is_follow = 0;
            }
            let data = {
                follow_id:$("#upload_user_id").val(),
                follower_id:current_user.user_id,
                is_follow:is_follow};
            let method = "POST";
            let url = "/follow/deal_follow";
            console.log(data);
            ajax_base(data, url, method, "Json").then(function (data) {
                    if (data.success) {
                        console.log(data);
                        if(follow == "关注"){
                            $("#follow").text("已关注");
                        }else{
                            $("#follow").text("关注");
                        }
                    }
                }
            );
        }else {
            alert("登录后才可以关注!");
        }
    });

    //提交举报
    $("#abuse_submit").click(function () {
        let option = parseInt($('.more_pop_info input[name="optionsRadios"]:checked ').val());
        console.log("提交举报"+option);
        if(current_user.user_id != 0){
            let data =  {
                "video_id":$("#video_id").val(),
                "user_id":$("#user_id").val(),
                "reason":option,};
            let method = "POST";
            let url = "/abuse/add_abuse";

            console.log(data);
            ajax_base(data, url, method, "Json").then(function (data) {
                    if (data.success) {
                        console.log(data);
                        $("#myModel").css("display","none");
                        alert("举报成功！");
                    }
                }
            );
        }else{
            alert("登录后才可以举报!");
        }
    });

};

//加载评论
let loadComments=function (video_id) {
    let data =  {
        "video_id":video_id,
       };
    let method = "GET";
    let url = "/comment/comments";
    console.log(data);
    result = ajax_base(data, url, method, "Json").then(function (data) {
            if (data.success) {
                comments = data.obj;
                console.log(comments);
                return data.obj;
            }
        }
    );
    return result;
};

//加载单条视频信息
let singleRun=function () {
    // let video = null;
    let url = "/video/singleData";
    let method = "GET";
    let data = {
        user_id:current_user.user_id,
        video_id: getUrlParam("video_id"),
    };
    ajax_base(data, url, method, "Json").then(function (data) {//同步请求
            if (data.success) {
                // console.log(data);
                let video = data.obj;
                console.log(video);

                let s1 = '<div class="song-info">'+
                    '   <h3 style="text-align: center;">'+video.title+'</h3>'+
                    '</div>'+
                    '<div class="video-grid">'+
                    '   <video id="my-video" class="video-js" controls preload="auto"'+
                    '           poster="'+video.image_url+'" data-setup="{}">'+
                    '       <source src="'+video.video_url+'" type="video/mp4">'+
                    '       <p class="vjs-no-js"> To view this video please enable JavaScript, and consider upgrading to a web browser that  <a href="http://videojs.com/html5-video-support/jsp.html" target="_blank">supports HTML5 video</a> </p>'+
                    '   </video>'+
                    '</div>';
                $(".song").append(s1);

                let follow = '关注';
                if(video.is_follow == 1){
                    follow = '已关注';
                }
                let s2 = '<h5 style="text-align: center">'+video.account+'</h5>'+
                    '<ul>'+
                    '   <li style="mar"><a href="" style="background:url('+video.user_image_url+') no-repeat center;background-size:100px 100px;width: 100px;height: 100px;display: block;border-radius: 50%;margin:0 auto"> </a></li>'+
                    '   <li><a class="icon like" id="follow">'+follow+'</a></li>'+
                    '   <li><a href="#sendComment" class="icon comment-icon">评论</a></li>'+
                    '   <li id="abuse_action" style="position: relative"><a href="javascript:;" class="icon pinterest-icon" id="add_abuse">举报</a></li>'+
                    '   <li class="view">'+video.views+' Views</li>'+
                    '</ul>'+
                    '<input type="hidden" value="'+video.user_id+'" id="upload_user_id">';
                $(".share").append(s2);

                let s3 = '<li style="display: list-item;">'+
                    '  <h4>Published on '+video.upload_date+'</h4>'+
                    '  <p>'+video.intro+'</p>'+
                    '</li>';
                $("#myList").append(s3);



                //修改评论表单内容
                $("#video_id").val(video.video_id);
                $("#user_id").val(current_user.user_id);

                //请求所有评论
                loadComments(video.video_id).then(function (data) {
                        let comment = data;
                        $("#total_comments").text('All Comments ('+comment.length+')');//评论总数

                        //加载评论
                        for(var i = 0; i < comment.length; i++) {
                            insertComment(comment[i],"append");
                        }
                    }
                );

                //显示举报的模态框
                abuseModelView();

                //处理请求
                dealGetAndPost();

            }
        }
    );

    // console.log(video)
};
singleRun();


